﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using ThoiTrang.Business;

public partial class Contact : System.Web.UI.Page
{
    protected bool isMessageMode = false;
    protected override void OnInit(EventArgs e)
    {
        btnSubmit.Click +=new EventHandler(btnSubmit_Click);        
        base.OnInit(e);
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(Request[FOConstants.QSP_CKID]))
            SessionManager.SetCookie(TTHelper.GetRandomCkid(Server.MapPath(string.Format("Files/ckid_latest.txt")), Request.ServerVariables["REMOTE_ADDR"]));
        else SessionManager.SetCookie(Request[FOConstants.QSP_CKID]);
        if (!Page.IsPostBack)
        {
            LoadDepartments();
            TTHelper.UpdateSession(Request[FOConstants.QSP_CKID]);
        }
        else
        {
            divEmail.Visible = false;
            divMsg.Visible = true;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            StreamReader reader = new StreamReader(Server.MapPath(string.Format("EmailTemplates/{0}.htm", ddlDepartments.SelectedValue)));
            string content = reader.ReadToEnd();
            EmailHelper.SendMail(txtSenderEmail.Text.Trim(), txtTitle.Text.Trim(), content);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

    protected void LoadDepartments()
    {
        //if (Page.IsPostBack) return;
        ddlDepartments.Items.Add(new ListItem("Ban Quản Trị (Management)", "Management"));
        ddlDepartments.Items.Add(new ListItem("Phòng Biên Tập (Editorial)", "Editorial"));
        ddlDepartments.Items.Add(new ListItem("Phòng Quảng Cáo (Advertising)", "Advertising"));
        ddlDepartments.Items.Add(new ListItem("Phòng Hỗ Trợ Bạn Đọc (Support)", "Support"));
        if (!String.IsNullOrEmpty(Request["r"])) ddlDepartments.SelectedValue = Request["r"];
    }
}
